package com.zh.note.huawei.onehundred.预定酒店;

import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/8/3 21:42
 * @description: create it
 */
public class Main {
    /**
     * 自定义排序规则
     *
     * @param args
     */
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int n = sc.nextInt();
        // int k = sc.nextInt();
        // int x = sc.nextInt();
        // int[] prices = new int[n];
        // for (int i = 0; i < n; i++) {
        //     prices[i] = sc.nextInt();
        // }

        int n = 10;
        int k = 4;
        int x = 6;
        int[] prices = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        //如果differ相同就去元素小的
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((o1, o2) -> {
            int differ1 = Math.abs(o1 - x);
            int differ2 = Math.abs(o2 - x);
            return differ1 == differ2 ? Integer.compare(o1, o2) : Integer.compare(differ1, differ2);
        });
        for (int price : prices) {
            priorityQueue.add(price);
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            list.add(priorityQueue.poll());
        }
        Collections.sort(list);
        System.out.println(list.toString());
    }
}
